Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reject or types matching multiple types for int literals #24012

Closed
wants to merge 2 commits into from

Conversation

metagn
Copy link
Collaborator

@metagn metagn commented Aug 25, 2024

fixes #4858, refs #12552

Testing CI first, might fix more issues (#6407 has another underlying issue)

@metagn
Copy link
Collaborator Author

metagn commented Aug 26, 2024

Package failures seem fine, the test failure is an array instantiation bug that I don't understand at all let alone how this triggers it, we can avoid it by swapping the operands of the == but I'll wait until at least after #24005 to try fixing it

@metagn
Copy link
Collaborator Author

metagn commented Aug 26, 2024

Sorry for misleading, it's not an array instantiation bug, it's an overflow caused by .. not picking this branch for range types anymore, it was just silenced since checks are off.

The reason is 0..1(int) matches int worse than the other integer types, giving isSubrange instead of isFromIntLit here. So I'll try to come up with some behavior here but it might be hard to not break stuff.

Edit: Seems to work

@metagn metagn force-pushed the ambiguous-int-conv branch from 03f16e4 to 2957aac Compare August 26, 2024 10:59
@metagn
Copy link
Collaborator Author

metagn commented Aug 27, 2024

This is a weird rule to justify to PR all these packages, or types just matching the first one they can makes sense. I think the range change is still important though, and #12552 should be fixed to fix #4858

@metagn metagn closed this Aug 27, 2024
Araq pushed a commit that referenced this pull request Aug 27, 2024
…ther int types (#24017)

This is a very niche case encountered in #24012, where an int literal
got a `range` type as a result of a generic instantiation (in
`tgenericcomputedrange`), I can't think of another test case. The base
type of the range being `int` made it match `int` with `isSubrange` as
in the first `if` branch, but other int types like `int32` matched with
`isFromIntLit` which is a better match.

Instead, int literals with range type now:

1. match their base type with `isFromIntLit`,
2. don't match other int types with `isFromIntLit`, instead giving
`isConvertible` as in the last `if` branch in `handleRange`.
Araq pushed a commit that referenced this pull request Oct 6, 2024
split from #24198

This is a required refactor for the only good solution I've been able to
think of for #4858 etc. Explanation:

---

`sigmatch` currently [disables
bindings](https://github.com/nim-lang/Nim/blob/d6a71a10671b66ee4f5be09f99234b3d834e7fce/compiler/sigmatch.nim#L1956)
(except for binding to other generic parameters) when matching against
constraints of generic parameters. This is so when the constraint is a
general metatype like `seq`, the type matching will not treat all
following uses of `seq` as the type matched against that generic
parameter.

However to solve #4858 etc we need to bind `or` types with a conversion
match to the type they are supposed to be converted to (i.e. matching
`int literal(123)` against `int8 | int16` should bind `int8`[^1], not
`int`). The generic parameter constraint binding needs some way to keep
track of this so that matching `int literal(123)` against `T: int8 |
int16` also binds `T` to `int8`[^1].

The only good way to do this IMO is to generate a new "binding context"
when matching against constraints, then binding the generic param to
what the constraint was bound to in that context (in #24198 this is
restricted to just `or` types & concrete types with convertible matches,
it doesn't work in general).

---

`semtypinst` already does something similar for bindings of generic
invocations using `LayeredIdTable`, so `LayeredIdTable` is now split
into its own module and used in `sigmatch` for type bindings as well,
rather than a single-layer `TypeMapping`. Other modules which act on
`sigmatch`'s binding map are also updated to use this type instead.

The type is also made into an `object` type rather than a `ref object`
to reduce the pointer indirection when embedding it inside
`TCandidate`/`TReplTypeVars`, but only on arc/orc since there are some
weird aliasing bugs on refc/markAndSweep that cause a segfault when
setting a layer to its previous layer. If we want we can also just
remove the conditional compilation altogether and always use `ref
object` at the cost of some performance.

[^1]: `int8` binding here and not `int16` might seem weird, since they
match equally well. But we need to resolve the ambiguity here, in #24012
I tested disallowing ambiguities like this and it broke many packages
that tries to match int literals to things like `int16 | uint16` or
`int8 | int16`. Instead of making these packages stop working I think
it's better we resolve the ambiguity with a rule like "the earliest `or`
branch with the best match, matches". This is the rule used in #24198.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unsigned integers in overloaded operators.
1 participant